function [f, g, h]=L_alpha_2(x, model, n)

% global model;
% global n;

N     = model.N;
k     = model.k;
k_hat = model.k_hat;

term1 = -sum(((model.alpha_02-x).^2)./(2*model.sigma_02.^2)); 
term2 = -sum(sum((repmat(model.alpha_02,N,1)-model.alpha_2).*((model.eta*model.alpha_1'))',1)./model.sigma_02.^2); 
term2 = term2*(model.lambda);
term3 = sum(x.*reshape(sum(model.phi_2(n,:,:),2),1,k_hat));
term4 = -(model.r2/model.zeta_2(n))*sum(exp(x+model.sigma_2(n,:).^2/2));

f     = -(term1+term2+term3+term4);

%% specifying the Jacobian

if nargout > 1
    
   term11 = (model.alpha_02-x)./(model.sigma_02.^2);
   term12 = (model.eta*model.alpha_1(n,:)')'./(model.sigma_02.^2);
   term12 = term12*(model.lambda);
   term13 = reshape(sum(model.phi_2(n,:,:),2),1,k_hat); 
   term14 = -model.r2/model.zeta_2(n)*exp(x+(model.sigma_2(n,:)).^2/2);

   g      = -(term11+term12+term13+term14);
   
   
   
   
   %% specifying the Hessian


   if nargout > 2
    
    term21 = -1./(model.sigma_02.^2);
    term22 = -(model.r2/model.zeta_2(n))*exp(x+model.sigma_2(n,:).^2/2);
    h      = -diag(term21+term22);
   
   end


end


end
